#!/usr/bin/env python3
"""
导入检查脚本 - 检查项目中的导入是否有问题
"""

import ast
import os
import sys
from pathlib import Path

def extract_imports(file_path):
    """提取文件中的导入语句"""
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
        
        tree = ast.parse(content)
        imports = []
        
        for node in ast.walk(tree):
            if isinstance(node, ast.Import):
                for alias in node.names:
                    imports.append(alias.name)
            elif isinstance(node, ast.ImportFrom):
                module = node.module or ''
                for alias in node.names:
                    imports.append(f"{module}.{alias.name}" if module else alias.name)
        
        return imports
    except Exception as e:
        return [f"ERROR: {e}"]

def main():
    """主函数"""
    print("=" * 60)
    print("Python 导入检查")
    print("=" * 60)
    
    # 要检查的文件列表
    files_to_check = [
        "app.py",
        "page/home.py", 
        "components/color_trigger.py",
        "utils/config_manager.py",
        "dialogs/color_picker.py",
        "dialogs/trigger_settings.py",
        "dialogs/smart_detection_dialog.py"
    ]
    
    all_imports = set()
    
    for file_path in files_to_check:
        if os.path.exists(file_path):
            print(f"\n📁 {file_path}:")
            imports = extract_imports(file_path)
            
            for imp in imports:
                if imp.startswith("ERROR"):
                    print(f"   ❌ {imp}")
                else:
                    print(f"   📦 {imp}")
                    all_imports.add(imp.split('.')[0])  # 只取顶级模块名
    
    print("\n" + "=" * 60)
    print("📋 所有导入的顶级模块:")
    for module in sorted(all_imports):
        if not module.startswith('ERROR'):
            print(f"   • {module}")
    
    print("\n" + "=" * 60)
    print("🔍 潜在问题检查:")
    
    # 检查可能的问题
    required_modules = ['PySide6', 'Pillow', 'pyautogui', 'qt_material']
    optional_modules = ['pynput', 'psutil']
    
    print("\n必需模块:")
    for module in required_modules:
        if module in all_imports or any(imp.startswith(module) for imp in all_imports):
            print(f"   ✅ {module}")
        else:
            print(f"   ❌ {module} (缺失)")
    
    print("\n可选模块:")
    for module in optional_modules:
        if module in all_imports or any(imp.startswith(module) for imp in all_imports):
            print(f"   ✅ {module}")
        else:
            print(f"   ⚠️  {module} (可选，但代码中有使用)")

if __name__ == "__main__":
    main()
